﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Ajax
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request.QueryString["username"] + "";
            string nickname = Request.QueryString["nickname"] + "";
            string password = Request.QueryString["password"] + "";
            string email = Request.QueryString["email"] + "";
            string captcode = Request.QueryString["captcode"] + "";
            string frienduserid = Request.QueryString["frienduserid"] + "";//推荐用户ID

            string code = Session["code"] + "";
            if (captcode != code)
            {
                Response.Write("2");
                return;
            }
            else
            {
                //注册
                Users uinfo = new Users();
                uinfo.UserName = username;
                uinfo.NickName = nickname;
                uinfo.Password = Encrypt.Encode(password);
                uinfo.Email = email;
                uinfo.RoleID = "3";
                uinfo.RoleType = 1;
                uinfo.LastAccessDate = System.DateTime.Now;
                uinfo.Flag = 0;
                //默认头像
                uinfo.SmallHeadImage = "/images/morentouxiang.jpg";
                UsersHelper uhelper = new UsersHelper();

                uhelper.AddUsers(uinfo);
                Session["CurrentUser"] = uinfo;

                //将信息记录到cookie中
                HttpCookie cookie = new HttpCookie("USER_COOKIE");
                cookie.Values.Add("username", uinfo.UserName);
                cookie.Values.Add("password", uinfo.Password);
                cookie.Expires = System.DateTime.Now.AddDays(30.0);
                HttpContext.Current.Response.Cookies.Add(cookie);

                if (frienduserid != "0")
                {
                    //判断推荐用户ID在系统中是否为正确的ID
                    Users frienduinfo = new Users();
                    frienduinfo = uhelper.GetUsers(frienduserid);
                    if (frienduinfo != null)
                    {
                        //将两个用户添加为关注关系
                        Friend finfo1 = new Friend();
                        Friend finfo2 = new Friend();
                        FriendHelper fhelper = new FriendHelper();
                        finfo1.Type = 0;
                        finfo1.UsersID = uinfo.ID;
                        finfo1.NickName = uinfo.NickName;
                        finfo1.FriendUsersID = frienduinfo.ID;
                        finfo1.FriendNickName = frienduinfo.NickName;

                        finfo2.Type = 0;
                        finfo2.UsersID = frienduinfo.ID;
                        finfo2.NickName = frienduinfo.NickName;
                        finfo2.FriendUsersID = uinfo.ID;
                        finfo2.FriendNickName = uinfo.NickName;

                        fhelper.AddFriend(finfo1);
                        fhelper.AddFriend(finfo2);

                        //更改用户表中关注的字段值
                        int userfriendnum = uinfo.FriendNum;//关注的人数
                        int usertobefriendnum = uinfo.ToBeFriendNum;//被关注的人数
                        uinfo.FriendNum = userfriendnum + 1;
                        uinfo.ToBeFriendNum = usertobefriendnum + 1;
                        uhelper.EditUsers(uinfo);

                        int friendfriendnum = frienduinfo.FriendNum;
                        int friendtobefriendnum = frienduinfo.ToBeFriendNum;
                        frienduinfo.FriendNum = friendfriendnum + 1;
                        frienduinfo.ToBeFriendNum = friendtobefriendnum + 1;
                        uhelper.EditUsers(frienduinfo);

                        SystemConfig scinfo = new SystemConfig();
                        SystemConfigHelper schelper = new SystemConfigHelper();
                        scinfo = schelper.GetSystemConfig("1");
                        int recommandregistemoneynum = scinfo.RecommandRegisterMoneyNum;
                        float recommandregistermoney = (float)scinfo.RecommandRegisterMoney;

                        if (recommandregistermoney > 0)
                        {
                            //为推荐注册的用户增加系统奖励
                            CashFlowHelper cfhelper = new CashFlowHelper();
                            Criteria c = new Criteria();
                            c.Add(CriteriaType.Equals, "UsersID", frienduinfo.ID);
                            c.Add(CriteriaType.Equals, "Type", 8);
                            c.Add(CriteriaType.Equals, "Description", "推荐好友注册");
                            int count = cfhelper.QueryCashFlowCountBySearch(c);
                            if (count < recommandregistemoneynum)
                            {
                                //增加奖励记录
                                CashFlow cfinfo = new CashFlow();
                                cfinfo.UsersID = frienduinfo.ID;
                                cfinfo.Money = recommandregistermoney;
                                cfinfo.Type = 8;
                                cfinfo.Description = "推荐好友注册";
                                cfhelper.AddCashFlow(cfinfo);
                            }
                        }
                    }
                }
                Response.Write("1");
                return;
            }
        }
    }
}